//
//  AttractionsViewController.m
//  chanyou
//
//  Created by wanli on 15/1/4.
//  Copyright (c) 2015年 wanli. All rights reserved.
//--------------------  旅行地详情 --------------------

#import "AttractionsViewController.h"
#import "ConnectModel.h"
#import "UIImageView+WebCache.h"
#import "AttractionsTableViewCell.h"
#import "MapViewController.h"
#import "MJRefresh.h"
#import "MJRefreshHeaderView.h"
#import "MJRefreshFooterView.h"
#import "SDImageCache.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
@interface AttractionsViewController ()

@end

@implementation AttractionsViewController
- (void)dealloc
{
    
    [_tableArray release];
    [_tableView release];
    [_AttractionsDic release];
    [_headList release];
    [_lat release];
    [_lng release];
    [_name_zh_cn release];
    [_headView release];
    [_destination_id release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSArray array];
        self.AttractionsDic = [NSDictionary dictionary];
        self.headList = [NSMutableArray array];
            self.navigationController.navigationBar.translucent = YES;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
//     self.navigationItem.title = @"";
    [self createTableView];
    [self startConnect];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(backAction)]autorelease];
}
- (void)createTableView{
    self.tableView = [[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain] autorelease];
    [_tableView registerClass:[AttractionsTableViewCell class] forCellReuseIdentifier:@"AttCell"];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];


    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    if (scrollView.contentOffset.y < 160) {
        _headView.alpha = 1 - scrollView.contentOffset.y / 180;
    }
    
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
    
    //    [[SDImageCache sharedImageCache] clearMemory];
}
- (void)startConnect{
    NSString *path = [NSString stringWithFormat:@"%@%@.json", @"http://chanyouji.com/api/attractions/", _destination_id];
    __block AttractionsViewController *att = self;
    [ConnectModel connectModelWithUrl:path parmaters:nil finished:^(NSData *data) {
        att.AttractionsDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        att.tableArray = [att.AttractionsDic objectForKey:@"attraction_trip_tags"];
        [att createTableHead];
        for (NSDictionary *dic in att.tableArray) {
            [att.headList addObject:[dic objectForKey:@"name"]];
        }
        [att.tableView reloadData];
    }];
}
- (void)createTableHead{
    self.lat = [_AttractionsDic objectForKey:@"lat"];
    self.lng = [_AttractionsDic objectForKey:@"lng"];
    self.name_zh_cn = [_AttractionsDic objectForKey:@"name_zh_cn"];
    
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2)];
    
    self.headView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 3)];

    [_headView sd_setImageWithURL:[NSURL URLWithString:[_AttractionsDic objectForKey:@"image_url"]] placeholderImage:[UIImage imageNamed:@"placeHold.png"]];
    
    
    [view addSubview:_headView];

    [_headView release];
    
  
    
    UILabel *name_zh_cn = [[UILabel alloc] initWithFrame:CGRectMake(60, self.view.frame.size.height / 3 - 70, 300, 30)];
    if ([_AttractionsDic objectForKey:@"name_zh_cn"] == nil || [[_AttractionsDic objectForKey:@"name_zh_cn"] isKindOfClass:[NSNull class]]) {
        name_zh_cn.text = @" ";
    }else{
        
        name_zh_cn.text = [_AttractionsDic objectForKey:@"name_zh_cn"];
    }

    name_zh_cn.font = [UIFont systemFontOfSize:24];
    [name_zh_cn setTextColor:[UIColor whiteColor]];
    [_headView addSubview:name_zh_cn];
    [name_zh_cn release];
    
    UILabel *name_en = [[UILabel alloc] initWithFrame:CGRectMake(30, self.view.frame.size.height / 3 - 40, 300, 30)];
    if ([_AttractionsDic objectForKey:@"name_en"] == nil || [[_AttractionsDic objectForKey:@"name_en"] isKindOfClass:[NSNull class]]) {
        name_en.text = @" ";
    }else{
        
        name_en.text = [_AttractionsDic objectForKey:@"name_en"];
    }
    
    name_en.font = [UIFont systemFontOfSize:18];
    [name_en setTextColor:[UIColor whiteColor]];
    [_headView addSubview:name_en];
    [name_en release];
    
    UIButton *mapButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [mapButton setBackgroundImage:[UIImage imageNamed:@"地图"] forState:UIControlStateNormal];
    mapButton.frame = CGRectMake(10, self.view.frame.size.height / 3 + 30, 30, 30);
    [mapButton addTarget:self action:@selector(mapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//    mapButton.buttonType = UIButtonTypeInfoDark;
    [view addSubview:mapButton];
    [mapButton release];
    
    UILabel *description = [[UILabel alloc] initWithFrame:CGRectMake(45, self.view.frame.size.height / 3 + 10, self.view.frame.size.width - 40, 60)];
    description.numberOfLines = 0;

    if ([_AttractionsDic objectForKey:@"description"] == nil || [[_AttractionsDic objectForKey:@"description"] isKindOfClass:[NSNull class]]) {
        description.text = @" ";
    }else{
        
        description.text = [_AttractionsDic objectForKey:@"description"];
    }
    description.font = [UIFont systemFontOfSize:15];
    [view addSubview:description];
    [description release];
    
    
    _tableView.tableHeaderView = view;
}
- (void)mapButtonAction:(id)sender{
    MapViewController *map = [[MapViewController alloc] init];
    map.lat = [_lat floatValue];
    map.lng = [_lng floatValue];
    map.name_zh_cn = _name_zh_cn;
    [self.navigationController pushViewController:map animated:YES];
        [[SDImageCache sharedImageCache] clearMemory];
    [map release];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [_headList count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
  
    
    return _headList[section];
   
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSDictionary *dic = [_tableArray objectAtIndex:section];
    NSArray *array = [dic objectForKey:@"attraction_contents"];
    return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AttractionsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AttCell"];
    NSDictionary *dic = [_tableArray objectAtIndex:indexPath.section];
    NSArray *array = [dic objectForKey:@"attraction_contents"];
    NSDictionary *contentDic = [array objectAtIndex:indexPath.row];
    
    cell.destinations.text = [contentDic objectForKey:@"description"];
    
    
    NSDictionary *dicc = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:18.0f], NSFontAttributeName, nil];
    CGRect rect = [cell.destinations.text boundingRectWithSize:CGSizeMake(340, 10000) options:(NSStringDrawingUsesLineFragmentOrigin| NSStringDrawingUsesFontLeading) attributes:dicc context:nil];
    
    
    
//    NSLog(@"cell = %f", cell.destinations.frame.size.height);
    cell.firstImage.frame = CGRectMake(10, rect.size.height + 10, self.view.frame.size.width - 20, 180);
    NSArray *picArray = [contentDic objectForKey:@"notes"];
    
    NSString *imageUrl = [[picArray firstObject] objectForKey:@"photo_url"];
    [cell.firstImage sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"placeHold.png"]];
    
 
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    NSLog(@"section = %ld", indexPath.section);
    //获得文本的高度
    NSDictionary *dicc = [_tableArray objectAtIndex:indexPath.section];
    NSArray *array = [dicc objectForKey:@"attraction_contents"];
    NSDictionary *contentDic = [array objectAtIndex:indexPath.row];
//    cell.destination.text = [contentDic objectForKey:@"description"];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:18.0f], NSFontAttributeName, nil];
    
    
    CGRect rect = [[contentDic objectForKey:@"description"] boundingRectWithSize:CGSizeMake(self.view.frame.size.width, 1000) options:(NSStringDrawingUsesLineFragmentOrigin| NSStringDrawingUsesFontLeading) attributes:dic context:nil];
    
    _height = rect.size.height + 230;

    return _height;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
